// 全局loading状态管理
class LoadingManager {
  constructor() {
    this.loading = false
    this.text = '加载中...'
    this.type = 'default'
    this.size = 'medium'
    this.listeners = []
  }

  // 显示loading
  show(options = {}) {
    this.loading = true
    this.text = options.text || '加载中...'
    this.type = options.type || 'default'
    this.size = options.size || 'medium'
    this.notifyListeners()
  }

  // 隐藏loading
  hide() {
    this.loading = false
    this.notifyListeners()
  }

  // 添加监听器
  addListener(callback) {
    this.listeners.push(callback)
  }

  // 移除监听器
  removeListener(callback) {
    const index = this.listeners.indexOf(callback)
    if (index > -1) {
      this.listeners.splice(index, 1)
    }
  }

  // 通知所有监听器
  notifyListeners() {
    this.listeners.forEach(callback => {
      callback({
        loading: this.loading,
        text: this.text,
        type: this.type,
        size: this.size
      })
    })
  }

  // 获取当前状态
  getState() {
    return {
      loading: this.loading,
      text: this.text,
      type: this.type,
      size: this.size
    }
  }
}

// 创建全局实例
const loadingManager = new LoadingManager()

// 导出实例和便捷方法
export default loadingManager

// 便捷方法
export const showLoading = (options) => loadingManager.show(options)
export const hideLoading = () => loadingManager.hide()
export const getLoadingState = () => loadingManager.getState() 